package com.qgzj.response;


import com.qgzj.exception.*;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @Description: ajax响应对象
 * @Author: yangyangbian
 * @CreateTime: 2018/4/19 11:18
 */
@ApiModel(value = "ajax响应对象", description = "ajax响应对象")
@Data
public class CommonRes<T extends Object> {

    @ApiModelProperty(value = "响应状态0-成功 其余失败", name = "status")
    private Integer status;

    @ApiModelProperty(value = "描述", name = "msg")
    private String msg;

    @ApiModelProperty(value = "内容", name = "data")
    private T data;

    public static CommonRes success(Object data) {
        CommonRes commonRes = new CommonRes();
        commonRes.setData(data);
        commonRes.setStatus(QgzjStudyExceptionEnum.SUCCESS.getStatus());
        commonRes.setMsg(QgzjStudyExceptionEnum.SUCCESS.getMessage());
        return commonRes;
    }

    public static CommonRes success() {
        CommonRes commonRes = new CommonRes();
        commonRes.setStatus(QgzjStudyExceptionEnum.SUCCESS.getStatus());
        commonRes.setMsg(QgzjStudyExceptionEnum.SUCCESS.getMessage());

        return commonRes;
    }
    
    public static CommonRes updateSuccess() {
        CommonRes commonRes = new CommonRes();
        commonRes.setStatus(QgzjStudyExceptionEnum.UPDATE_SUCCESS.getStatus());
        commonRes.setMsg(QgzjStudyExceptionEnum.UPDATE_SUCCESS.getMessage());

        return commonRes;
    }
    
    public static CommonRes addSuccess() {
        CommonRes commonRes = new CommonRes();
        commonRes.setStatus(QgzjStudyExceptionEnum.ADD_SUCCESS.getStatus());
        commonRes.setMsg(QgzjStudyExceptionEnum.ADD_SUCCESS.getMessage());

        return commonRes;
    }

    public static CommonRes updateError() {
        CommonRes commonRes = new CommonRes();
        commonRes.setStatus(QgzjStudyExceptionEnum.UPDATE_ERROR.getStatus());
        commonRes.setMsg(QgzjStudyExceptionEnum.UPDATE_ERROR.getMessage());

        return commonRes;
    }
    
    public static CommonRes addError() {
        CommonRes commonRes = new CommonRes();
        commonRes.setStatus(Integer.valueOf(QgzjStudyExceptionEnum.ADD_ERROR.getStatus()));
        commonRes.setMsg(QgzjStudyExceptionEnum.ADD_ERROR.getMessage());

        return commonRes;
    }

    public static CommonRes systemFail(Object data){
        CommonRes res = new CommonRes();
        res.setStatus(QgzjStudyExceptionEnum.FAIL.getStatus());
        res.setMsg(QgzjStudyExceptionEnum.FAIL.getMessage());
        res.setData(data);
        return res;
    }

    public static CommonRes fail(QgzjException qe){
        CommonRes res = new CommonRes();
        res.setStatus(qe.getCode());
        res.setMsg(qe.getMsg());
        return res;
    }

    public static CommonRes fail(QgzjException qe, Object data){
        CommonRes res = new CommonRes();
        res.setStatus(qe.getCode());
        res.setMsg(qe.getMsg());
        res.setData(data);
        return res;
    }

}